package com.fun.feifun.modules.sys.parameter.controller;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RestController;
import com.fun.feifun.modules.sys.parameter.service.ISysParameterService;
import com.fun.feifun.modules.sys.parameter.model.SysParameter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.fun.feifun.base.result.Result;
import io.swagger.annotations.ApiOperation;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;
import io.swagger.annotations.Api;

import java.util.List;

/**
 * <p>
 * 系统参数 前端控制器
 * </p>
 *
 * @author wkf
 * @since 2024-06-19
 */
@RestController
@Api(value = "/sys.parameter/sys-parameter", tags = "系统参数")
@Slf4j
@RequestMapping("/sys.parameter/sys-parameter")
    public class SysParameterController {


    @Autowired
    private ISysParameterService service;

    /**
     * 获取分页
     */
    @PostMapping("/page")
    @ApiOperation(value = "获取分页信息")
    public Result<Page<SysParameter>> page(@RequestParam(name = "page", defaultValue = "1") int index,@RequestParam(name = "size", defaultValue = "20") int size){
        Page page = new Page(index,size);
        service.page(page);
        return Result.success(page);
    }



    /**
     * 添加数据
     */
    @PostMapping(value = "/save")
    @ApiOperation(value = "添加或者保存")
    public Result save(@RequestBody SysParameter item){
        if(service.saveOrUpdate(item)){
         return Result.success("数据添加成功！");
        }
        return Result.err("数据添加成功！");
    }



    /**
     * 删除数据
     */
    @DeleteMapping("/physicalDel")
    @ApiOperation(value = "删除")
    public Result deleteItems(@RequestBody List<Long> ids){
        if(service.removeByIds(ids)){
            return Result.success("数据删除成功！");
        }
        return Result.err("数据删除失败");
    }



    /**
     * 获取全部数据
     */
    @GetMapping("/all")
    @ApiOperation(value = "获取全部数据")
    public Result<List<SysParameter>> getAll(){
        List<SysParameter> all = service.list();
        return Result.success(all);
    }

    /**
     * 根据ID查找数据
     */
    @GetMapping("/findById")
    @ApiOperation(value = "查找")
    public Result findById(@RequestParam("id") Long id){
        SysParameter item = service.getById(id);
        if(item==null){
            return Result.err("尚未查询到此ID");
        }
        return Result.success(item);
    }

    /**
     * 根据ID查找数据
     */
    @GetMapping("/getByCode")
    @ApiOperation(value = "查找")
    public Result getByCode(@RequestParam("code") String code){
        QueryWrapper<SysParameter> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(SysParameter::getCode,code);
        SysParameter item = service.getOne(queryWrapper);
        if(item==null){
            return Result.err("尚未查询到此ID");
        }
        return Result.success(item);
    }


    @GetMapping("/setByCode")
    @ApiOperation(value = "根据code修改")
    public Result setByCode(@RequestParam("code") String code,@RequestParam("value") String value){
        UpdateWrapper<SysParameter> wrapper = new UpdateWrapper<>();
        wrapper.lambda().eq(SysParameter::getCode,code).set(SysParameter::getValue,value);
        boolean update = service.update(wrapper);
        if(update){
            return Result.err("");
        }
        return Result.success();
    }
}

